
export const MOUNT_ENTRY_FUNCTION = 'onWork'


export default class LoopObject implements ILoopObject {
    name: string
    constructor(name: string) {
        this.name = name
    }
    loop(phase: string): void {
        switch (phase) {
            case 'born':
                this.born()
                break
            case 'reset':
                this.reset()
                break
            case 'tickStart':
                this.tickStart()
                break
            case 'do':
                this.do()
                break
            case 'afterWork':
                this.afterWork()
                break
            case 'tickEnd':
                this.tickEnd()
                break
        }
    }
    born(): void {
        // console.log(this.name, 'born()')
    }
    reset(): void {
        // console.log(this.name, 'reset()')
    }
    tickStart(): void {
        // console.log(this.name, 'tickStart()')
    }
    do(): void {
        // console.log(this.name, 'do()')
    }
    afterWork(): void {
        // console.log(this.name, 'afterWork()')
    }
    tickEnd(): void {
        // console.log(this.name, 'tickEnd()')
    }

    /**
     * 默认的异常捕获
     */
    protected _catcher: ErrorCatcher = next => {
        try {
            next()
        }
        catch (e) {
            console.log(`<span style="color:#ef9a9a">${e}</sapn>`)
            Game.notify(e)
        }
    }

    /**
     * 在异常捕获下执行对象的入口方法
     * @param gameItem 要调用入口方法的游戏对象
     */
     protected doWithCatcher(gameItem): void {
        this._catcher(gameItem[MOUNT_ENTRY_FUNCTION].bind(gameItem))
    }
}

